// index.js
const httpUtils = require('../../utils/httpUtil')
const ui = require('../../utils/ui')

Page({
  data: {
    background: [{
      id: 0,
      url: '/images/banner1.jpg'
    }, {
      id: 1,
      url: '/images/banner2.jpg'
    }, {
      id: 2,
      url: '/images/banner3.jpg'
    }],
    monitorDataList: [],  // 全部数据
    displayList: [],    // 当前展示的数据
    showAll: true,
    isRefresh: true,
    station: {
      // code: '6071XA10',
      // name: '测试站点',
      // measurementList: [
        // {
        //   type: '雨量',
        //   date: '2025-04-23',
        //   latestLevel: 12,
        //   unit: 'ml',
        // },
        // {
        //   type: '雨量',
        //   date: '2025-04-23',
        //   latestLevel: 12,
        //   unit: 'ml',
        // }
      // ]
    },
    deviceStatistic: {
      totalCount: 0,
      onlineCount: 0,
      breakdownCount: 0,
    },
    templateClassify: [],
  },

  onLoad() {
    this.getMonitorDataList();
    this.getDeviceStat();
    this.getModelStat();
  },

  onPullDownRefresh() {
    this.setData({
      isRefresh: true,
    });
    this.getMonitorDataList();
  },

  showMore() {
    wx.navigateTo({
      url: '/pages/home/measure-data-list/measure-data-list',
    })
  },

  jump2DeviceList() {
    wx.navigateTo({
      url: '/pages/home/device-list/device-list',
    })
  },

  jump2TemplateList() {
    wx.navigateTo({
      url: '/pages/home/template-list/template-list',
    })
  },

  getMonitorDataList() {
    httpUtils.request({
      'method': 'GET',
      'url': '/prod-api/interface/getMonitorData',
      // 'showLoading': true,
      // 'message': '获取监测数据...'
    }).then(res=>{
      this.setData({
        isRefresh: false,
      });
      if (res.data.code == 200) {
        let row = res.data.rows[0];
        this.setData({
          station: {
            code: row.measurementStationNo,
            name: row.stationName,
            measurementList: row.monitorDataList.map(mesurement=>{
              return {
                type: mesurement.measurementStationType,
                date: mesurement.synchronizeTimeStr,
                latestLevel: mesurement.latestLevel,
                unit: mesurement.unit,
                status: mesurement.statusFlag,
              }
            }),
          },
        });
      }
    });
  },

  getDeviceStat() {
    httpUtils.request({
      'method': 'GET',
      'url': '/prod-api/interface/getDeviceStat',
    }).then(res=>{
      this.setData({
        deviceStatistic: {
          totalCount: res.data.deviceCount,
          onlineCount: res.data.deviceOnline,
          breakdownCount: res.data.deviceFault,
        },
      })
    });
  },

  getModelStat() {
    httpUtils.request({
      'method': 'GET',
      'url': '/prod-api/interface/getModelStat',
    }).then(res=>{
      const entries = Object.entries(res.data).map(([key, value]) => {
        return { key, value };
      });
      this.setData({
        templateClassify: entries,
      })
    });
  },

  onMeasurementClick(e) {
    let station = e.currentTarget.dataset.station;
    if (station.status == '1' || station.status == '2' || station.status == '3') {
      wx.navigateTo({
        url: '/pages/call/wav-call/wav-call?measurementStationNo='+station.code,
      });
    }
  },

});
